package com.adk.backend.service.impl;

import com.adk.backend.common.PageResult;
import com.adk.backend.entity.LxWhList;
import com.adk.backend.mapper.WhListMapper;
import com.adk.backend.service.WhListService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 仓库列表服务实现类
 */
@Slf4j
@Service
public class WhListServiceImpl extends ServiceImpl<WhListMapper, LxWhList> implements WhListService {
    
    @Autowired
    private WhListMapper whListMapper;
    
    @Override
    public PageResult<LxWhList> getWhList(Map<String, Object> params) {
        Integer pageNum = (Integer) params.getOrDefault("pageNum", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 20);
        
        QueryWrapper<LxWhList> wrapper = new QueryWrapper<>();
        
        // 按类型筛选
        if (params.get("type") != null) {
            wrapper.eq("type", params.get("type"));
        }
        
        // 按父级ID筛选
        if (params.get("parentId") != null) {
            wrapper.eq("parent_id", params.get("parentId"));
        }
        
        // 按仓库名称模糊查询
        if (params.get("whName") != null && !params.get("whName").toString().isEmpty()) {
            wrapper.like("wh_name", params.get("whName"));
        }
        
        // 按仓库代码查询
        if (params.get("whCode") != null && !params.get("whCode").toString().isEmpty()) {
            wrapper.eq("wh_code", params.get("whCode"));
        }
        
        wrapper.orderByAsc("id");
        
        Page<LxWhList> page = new Page<>(pageNum, pageSize);
        Page<LxWhList> result = whListMapper.selectPage(page, wrapper);
        
        PageResult<LxWhList> pageResult = new PageResult<>();
        pageResult.setList(result.getRecords());
        pageResult.setTotal(result.getTotal());
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setTotalPages((int) result.getPages());
        
        return pageResult;
    }
    
    @Override
    public LxWhList getWhById(Long id) {
        return whListMapper.selectById(id);
    }
    
    @Override
    public LxWhList getWhByCode(String whCode) {
        QueryWrapper<LxWhList> wrapper = new QueryWrapper<>();
        wrapper.eq("wh_code", whCode);
        return whListMapper.selectOne(wrapper);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public LxWhList createWh(LxWhList whList) {
        whListMapper.insert(whList);
        return whList;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateWh(LxWhList whList) {
        return whListMapper.updateById(whList) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteWh(Long id) {
        return whListMapper.deleteById(id) > 0;
    }
    
    @Override
    public List<LxWhList> getAllWhList(Integer type) {
        QueryWrapper<LxWhList> wrapper = new QueryWrapper<>();
        
        if (type != null) {
            wrapper.eq("type", type);
        }
        
        wrapper.orderByAsc("id");
        
        return whListMapper.selectList(wrapper);
    }
    
    @Override
    public List<LxWhList> getWhListByParentId(Long parentId) {
        QueryWrapper<LxWhList> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        wrapper.orderByAsc("id");
        return whListMapper.selectList(wrapper);
    }
}

